<?php
	session_start();
	
	if (!isset($_SESSION['sesi_login']))
		header("Location:../../index.php");
	
	@chdir("../../");
	define('ABS_URL',str_replace("\\","/",getcwd()));
	require ABS_URL.'/engine/Smarty.class.php';
	require ABS_URL."/functions/class.query.inc.php";
	$smarty = new Smarty();
	require ABS_URL."/dev_con.inc.php";
	require ABS_URL."/functions/func_file.php";
	$queryData = new queryData();
	$queryData->baseConfig();
	require ABS_URL."/functions/func_content.php";
	require ABS_URL."/langs/".BASE_LANGUAGE.'.inc.php';
	require $queryData->get_curr_dir(dirname(__FILE__))."/langs/".BASE_LANGUAGE.'.inc.php';
	
	$kategori = strip_tags(trim($_POST['kategori']));
	$subkategori = trim($_POST['subkategori']);
	$type = strtolower($_POST['type']);
	//$fileImage = $_FILES['fileImage']['name'];
	$description = trim($_POST['deskripsi']);
	$publish = trim($_POST['publish']);
	$url = trim($_POST['url']);
	($publish=="ya") ? $publish = 1 : $publish = 0;
		
/* 
	function getFileExtension($str) {
        $i = strrpos($str,".");
        if (!$i) { return ""; }
        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
        return $ext;
    }

	$aext = getFileExtension($fileImage);
	$aext = strtolower($aext);
*/
		
	if (strtolower(ACT_VER)=="hapus") {
		$kode = (int) $_GET['kode'];
		
		//$sqlQ = "SELECT * FROM ikadir_kat WHERE id_dirkat='".$kode."'";
		//$qryQ = mysql_query($sqlQ);
		//$rowQ = mysql_fetch_object($qryQ);

		//$filenameEdit = $rowQ->picture;
		//$datakuEdit = "../../data/category_pic/".$filenameEdit;
		
		//@unlink($datakuEdit);
		
		$sql = "DELETE FROM ikadir_kat WHERE id_dirkat='".$kode."'";
		$qry = mysql_query($sql);

		$sql = "UPDATE ikadir_kat SET id_dirkatsub='0' WHERE id_dirkatsub='".$kode."'";
		$qry = mysql_query($sql);

		($qry) ? $_SESSION['sesi_ver'] = $queryData->cekAksi($kode,'delete') : $_SESSION['sesi_ver'] = '';
		$act = 'lihat';
	} else {		
		$a = array($kategori,$subkategori);
		$b = array('empty','sameDir');
		$c = array($DIRECTORY_TITLE,$DIRECTORY_FAIL_SAMEDIR);
		switch (strtolower($_POST['act_ver'])) {
			case "input" :
				$result = $queryData->NotificationCheck($a,$b,$c);
				if (empty($result)) {
				//$ex = array("jpg","jpeg","gif");
					
				//if (in_array($aext,$ex)) {
					
					//$filename = "IMG".date("dMYHis").".".$aext;
					//$dataku = "../../data/category_pic/".$filename;
					
					//if (move_uploaded_file($_FILES['fileImage']['tmp_name'],$dataku)) {
					$sql = "INSERT INTO ikadir_kat VALUES(NULL,'".$subkategori."','".$kategori."','".$type."','".$filename."','".$url."','".$description."','".$publish."')";
		
					$qry = mysql_query($sql);
					$_SESSION['sesi_ver'] = $queryData->cekAksi($kategori,'add');
					$act = 'lihat';
				} else {
					$_SESSION['sesi_ver'] = $DEV_ACTION_NOTIFICATION;
					foreach($result as $data) {
						$_SESSION['sesi_ver'] .= '<br />- '.$data;
					}
					$act = 'tambah';
				}
					//} else {
					//	$_SESSION['sesi_ver'] = "Image \"".$filename."\" cannot be uploaded";
					//}
				//} else {
					//$_SESSION['sesi_ver'] = "Directory \"".$filename."\" image must be JPG and GIF format only";
				//}
			break;
			case "edit" :
				$kode = (int) $_POST['kode'];
				$result = $queryData->NotificationCheck($a,$b,$c);
				if (empty($result)) {
				
				/*if (!empty($fileImage)) {
					$sqlQ = "SELECT * FROM ikadir_kat WHERE id_dirkat='".$kode."'";
					$qryQ = mysql_query($sqlQ);
					$rowQ = mysql_fetch_object($qryQ);
		
					$filenameEdit = $rowQ->picture;
					$datakuEdit = "../../data/category_pic/".$filenameEdit;
					
					$ex = array("jpg","jpeg","gif");
						
					if (in_array($aext,$ex)) {
						
						$filename = "IMG".date("dMYHis").".".$aext;
						$dataku = "../../data/category_pic/".$filename;
						
						if (move_uploaded_file($_FILES['fileImage']['tmp_name'],$dataku)) {
					
							@unlink($datakuEdit);
				
							$sql = "UPDATE ikadir_kat SET id_dirkatsub='".$subkategori."', dir_kat='".$kategori."', tipe='".$type."', picture='".$filename."', description='".$description."', url='".$url."', publish='".$publish."' WHERE id_dirkat='".$kode."'";
							$qry = mysql_query($sql);
							
							$_SESSION['sesi_ver'] = "Directory \"".$kategori."\" has been added";
						} else {
							$_SESSION['sesi_ver'] = "Image \"".$filename."\" cannot be uploaded";
						}
					} else {
						$_SESSION['sesi_ver'] = "Directory \"".$filename."\" image must be JPG and GIF format only";
					}
				} else {
					*/
					$sql = "UPDATE ikadir_kat SET id_dirkatsub='".$subkategori."', dir_kat='".$kategori."', tipe='".$type."', url='".$url."', description='".$description."',publish='".$publish."' WHERE id_dirkat='".$kode."'";
					$qry = mysql_query($sql);
				//}
					$_SESSION['sesi_ver'] = $queryData->cekAksi($kategori,'edit');
					$act = 'lihat';
				} else {
					$_SESSION['sesi_ver'] = $DEV_ACTION_NOTIFICATION;
					foreach($result as $data) {
						$_SESSION['sesi_ver'] .= '<br />- '.$data;
					}								
					$act = 'edit&kode='.$kode;
				}
			break;
		};
	};
	
	if (isset($_SESSION['sesi_login'])) {
		header('Location:../../?show='.SHOW.'&showview='.SHOWVIEW.'&act='.$act.'&limitdown=0');
	} else {
		header("Location:../../index.php");
	};
?>